package site.zhouinfo.function;

/**
 * 递归进行高阶函数运算
 *
 * @author zhou
 * @create 2017-02-08 17:00
 * @email zhouinfo@qq.com
 */
public class RecursionHigherFunction {

    public static void main(String[] args) {
        System.out.println(getIdentity(1, 10));
        System.out.println(getSquare(1, 10));
        System.out.println(getCube(1, 10));
    }

    public static int getIdentity(int a, int b) {
        HighFunInterface identityFun = x -> x;
        return intermediate(identityFun, a, b);
    }

    public static int getSquare(int a, int b) {
        HighFunInterface squareFun = x -> x * x;
        return intermediate(squareFun, a, b);
    }

    public static int getCube(int a, int b) {
        HighFunInterface cubeFun = x -> x * x * x;
        return intermediate(cubeFun, a, b);
    }

    public static int intermediate(HighFunInterface fun, int a, int b) {
        HighFunInterface next = x -> x + 1;
        return sum(fun, next, a, b);
    }

    public static int sum(HighFunInterface fun, HighFunInterface next, int a, int b) {
        if (a > b) {
            return 0;
        } else {
            return fun.opera(a) + sum(fun, next, next.opera(a), b);
        }
    }


}
